package cn.fl.busConsult.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.utils.DateUtils;
import cn.fl.busConsult.service.IPrjPrdQuatBusService;
import cn.fl.busConsult.vo.PrjConsultBusPrpsRecordVO;
import cn.fl.busConsult.vo.PrjPrdQuatBusVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.preloan.condition.PrjConsultBusPrpsRecordQueryCondition;
import cn.fl.preloan.constants.DictionaryConstant;
import cn.fl.preloan.constants.FlBaseConstant;
import cn.fl.system.service.ISysRegionService;
import com.alibaba.fastjson.JSON;
import org.apache.commons.httpclient.util.DateParseException;
import org.apache.commons.httpclient.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;

/**
 * @Description: 客车咨询报价主表 控制层
 */
@Controller
@RequestMapping("/bus/prjprdquat")
public class PrjPrdQuatBusController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PrjPrdQuatBusController.class);

	/**
	 * PrjPrdQuatBus 服务层BEAN
	 */
	@Autowired
	private IPrjPrdQuatBusService prjPrdQuatBusService;
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	@Autowired
	private ISysRegionService sysRegionService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toConsultRecordListPage.do", method = { RequestMethod.GET})
	public ModelAndView toConsultRecordListPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

		ModelAndView view = new ModelAndView("busConsult/consultRecordList");
		//行业
		List<KeyValueVO> professionList = sysDictionaryApiService.getKvList(DictionaryConstant.CODE_BUS_CONSULT_PROFESSION,FlBaseConstant.SYS_SYS_CD, FlBaseConstant.CODE_ORG_CD_KCRZ);
		view.addObject("professionList",professionList);
		//区域
		List<KeyValueVO> areaList = sysDictionaryApiService.getKvList(DictionaryConstant.CODE_REGION_CD,FlBaseConstant.SYS_SYS_CD, "00000008");
		view.addObject("areaList",areaList);
		//所在省份
		List<KeyValueVO> provinceList = this.sysRegionService.searchProvinceKVList();
		view.addObject("provinceList",provinceList);
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}


	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initConsultRecordList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initConsultRecordList(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "initList", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());


		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String profession = request.getParameter("profession");
		String name = request.getParameter("name");
		String prCd = request.getParameter("prCd");
		String areaNm = request.getParameter("areaNm");
		String crtTm = request.getParameter("crtTm");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		PrjConsultBusPrpsRecordQueryCondition qc = new PrjConsultBusPrpsRecordQueryCondition();
		if (StringUtils.isNotEmpty(profession)) {
			qc.setProfession(profession);
		}
		if (StringUtils.isNotEmpty(name)) {
			qc.setName(name);
		}
		if (StringUtils.isNotEmpty(prCd)) {
			qc.setPrCd(prCd);
		}
		if (StringUtils.isNotEmpty(areaNm)) {
			qc.setAreaNm(areaNm);
		}
		if (StringUtils.isNotEmpty(crtTm)) {
			try {
				qc.setCrtTm(DateUtil.parseDate(crtTm));
			} catch (DateParseException e) {
				e.printStackTrace();
			}
		}
		page.setCondition(qc);

		Pager pager = this.prjPrdQuatBusService.selectConsultRecordList(page);
		List<PrjConsultBusPrpsRecordVO> list = (List<PrjConsultBusPrpsRecordVO>) pager.getResultList();
		ListPageVO<PrjConsultBusPrpsRecordVO> listPage = new ListPageVO<PrjConsultBusPrpsRecordVO>(list, pager.getTotalCount());
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "initList", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 导出
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/exportBusConsultList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object exportBusConsultList(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "exportBusConsultList", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		String profession = request.getParameter("profession");
		String name = request.getParameter("name");
		String prCd = request.getParameter("prCd");
		String areaNm = request.getParameter("areaNm");
		String crtTm = request.getParameter("crtTm");

		PrjConsultBusPrpsRecordQueryCondition qc = new PrjConsultBusPrpsRecordQueryCondition();
		if (StringUtils.isNotEmpty(profession)) {
			qc.setProfession(profession);
		}
		if (StringUtils.isNotEmpty(name)) {
			qc.setName(name);
		}
		if (StringUtils.isNotEmpty(prCd)) {
			qc.setPrCd(prCd);
		}
		if (StringUtils.isNotEmpty(areaNm)) {
			qc.setAreaNm(areaNm);
		}
		if (StringUtils.isNotEmpty(crtTm)) {
			try {
				qc.setCrtTm(DateUtil.parseDate(crtTm));
			} catch (DateParseException e) {
				e.printStackTrace();
			}
		}
		return this.prjPrdQuatBusService.exportBusConsultList(qc);
	}


}

